﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using ArtClub.DBAL;

namespace ArtClub.Controller
{
    public class PieceCtr
    {
        private DataDiagramDataContext data = new DataDiagramDataContext();
        
        public Piece addPiece(string Name, string Artist, string Description, string Type, double OPrice, string Image, string CreateBy, DateTime Date)
        {
            Piece p = new Piece();
            p.name = Name;
            p.image = Image;
            p.originalPrice = OPrice;
            p.createdBy = CreateBy;
            p.date = Date;
            
            p.artist = Artist;
            p.type = Type;

            p.description = Description;
            data.Pieces.InsertOnSubmit(p);
            data.SubmitChanges();
            return p;
        }

        public void deletePiece(int id)
        {
            Piece p = data.Pieces.Where(c => c.id == id).First();
            data.Pieces.DeleteOnSubmit(p);
              data.SubmitChanges();
        }

        public Piece getPiece(int id)
        {
										Piece p = data.Pieces.Where(c => c.id == id).First();
										
									
          return p;
        }

        public Piece updatePiece(int id, string Name, string Artist, string Description, string Type, double OPrice, string Image, string CreateBy, DateTime Date)
        {
									
            Piece p = getPiece(id);
            p.name = Name;
            p.image = Image;
            p.originalPrice = OPrice;
            p.createdBy = CreateBy;
            p.date = Date;
            
            p.artist = Artist;
            p.type = Type;
            p.description = Description;
            
            data.SubmitChanges();
            return p;
        }
        
        public Piece[] getAllPieces()
        {
            var table = data.GetTable<Piece>();
            return table.ToArray();
        }
    }
}